package view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.text.R;

import java.util.List;

import data.SharedPreferencesUtils;
import entity.Home;
import util.BitmapUtil;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.LogUtil;

public class MorePopWindow extends PopupWindow {
	private View conentView;
	private Context mContext;
	private boolean mjudge = true;  //判断是否显示支付条目
	private LinearLayout mLLayout1,mLLayout2,mLLayout3,mLLayout4;
	private TextView mTV1,mTV2,mTV3,mTV4,mTV;
	private ImageView mImageView1,mImageView2,mImageView3,mImageView4;
    private List<Home.ComboBoxTapBarBean> mComboBoxTapBarBeen;
	private MorePopWindow mMorePopWindow;
	public MorePopWindow(final Activity context,List<Home.ComboBoxTapBarBean> comboBoxTapBarBeen) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.more_popup_dialog, null);

		setPopWindow(context);
		this.mComboBoxTapBarBeen = comboBoxTapBarBeen;
		this.mContext = context;
		mMorePopWindow = this;
		LogUtil.eE("BarBeen",""+mComboBoxTapBarBeen.size());
         if(mComboBoxTapBarBeen.size()<2){
	             return;
         }
		mLLayout1 = (LinearLayout)conentView.findViewById(R.id.linear_popup_1);
		mLLayout2 = (LinearLayout)conentView.findViewById(R.id.linear_popup_2);
		mLLayout3 = (LinearLayout)conentView.findViewById(R.id.linear_popup_3);
		mLLayout4 = (LinearLayout)conentView.findViewById(R.id.linear_popup_4);

		mTV1 = (TextView)conentView.findViewById(R.id.text_popup_1);
		mTV2 = (TextView)conentView.findViewById(R.id.text_popup_2);
		mTV3 = (TextView)conentView.findViewById(R.id.text_popup_3);
		mTV4 = (TextView)conentView.findViewById(R.id.text_popup_4);
		mTV = (TextView)conentView.findViewById(R.id.text_popup);

		mImageView1 = (ImageView)conentView.findViewById(R.id.image_popup_1);
		mImageView2 = (ImageView)conentView.findViewById(R.id.image_popup_2);
		mImageView3 = (ImageView)conentView.findViewById(R.id.image_popup_3);
		mImageView4 = (ImageView)conentView.findViewById(R.id.image_popup_4);

		if(mComboBoxTapBarBeen.size()>0){
			mTV1.setText(mComboBoxTapBarBeen.get(0).getTitle());
		}

		if(mComboBoxTapBarBeen.size() <=1){
			mLLayout2.setVisibility(View.GONE);
		}else {
			mTV2.setText(mComboBoxTapBarBeen.get(1).getTitle());
		}

		if(mComboBoxTapBarBeen.size() <=2 ){
			mLLayout3.setVisibility(View.GONE);
		}else {mTV3.setText(mComboBoxTapBarBeen.get(2).getTitle());}

		if(mComboBoxTapBarBeen.size() <= 3 ){mLLayout4.setVisibility(View.GONE);
		}else {mTV4.setText(mComboBoxTapBarBeen.get(3).getTitle());}

		if( mComboBoxTapBarBeen.size()>0){
			if(!mComboBoxTapBarBeen.get(0).getIcon().equals("")){
				BitmapUtil.newBitmap().showBitmap(mContext,mComboBoxTapBarBeen.get(0).getIcon(),mImageView1,R.mipmap.homepage_icon,null);
			}else {setImageBa(0,mImageView1);}
		}

		if(mComboBoxTapBarBeen.size() <=1){

		}else {
			if(!mComboBoxTapBarBeen.get(1).getIcon().equals("")){
				BitmapUtil.newBitmap().showBitmap(mContext,mComboBoxTapBarBeen.get(1).getIcon(),mImageView2,R.mipmap.homepage_icon,null);
			}else {setImageBa(0,mImageView2);}
		}

		if(mComboBoxTapBarBeen.size() <=2){

		}else {
			if(!mComboBoxTapBarBeen.get(2).getIcon().equals("")){
				BitmapUtil.newBitmap().showBitmap(mContext,mComboBoxTapBarBeen.get(2).getIcon(),mImageView3,R.mipmap.homepage_icon,null);
			}else {setImageBa(0,mImageView3);}
		}


		if(mComboBoxTapBarBeen.size() <= 3){}else {
			if(!mComboBoxTapBarBeen.get(3).getIcon().equals("")){
				BitmapUtil.newBitmap().showBitmap(mContext,mComboBoxTapBarBeen.get(3).getIcon(),mImageView4,R.mipmap.homepage_icon,null);
			}else {setImageBa(0,mImageView4);}
		}

		mLLayout1.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {setOnClic(0);}});

		mLLayout2.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {setOnClic(1);}});

		mLLayout3.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {setOnClic(2);}});

		mLLayout4.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {setOnClic(3);}});

	}

	/**
	 *设置图片
     */
	private void setImageBa(int i,ImageView imageView) {
		if(mComboBoxTapBarBeen.get(i).getType().equals("zhifu")){
			imageView.setBackgroundResource(R.mipmap.popup_about);
		}else if(mComboBoxTapBarBeen.get(i).getType().equals("saoyisao")){
			imageView.setBackgroundResource(R.mipmap.popup_share);
		}else if(mComboBoxTapBarBeen.get(i).getType().equals("baozhang")){
			imageView.setBackgroundResource(R.mipmap.popup_cooperation);
		}else if(mComboBoxTapBarBeen.get(i).getType().equals("tianjiahaoyou")){
			imageView.setBackgroundResource(R.mipmap.popup_cooperation);
		}
	}

	/**
	 * 显示popupWindow
	 *
	 */
	public void showPopupWindow(View parent) {
		if (!this.isShowing()){
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 0);
		} else {
			this.dismiss();
		}
	}

	/**
	 * 初始化PopWindow
	 */
	public void setPopWindow(Activity context){
		int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w / 2 - 20);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimationPreview);
		// 刷新状态
		this.update();
		LogUtil.eE("Window","setPopWindow");
	}

	/**
	 * 各点击事件
	 * @param position 点击的第几个
     */
	private void setOnClic(int position){
		mMorePopWindow.dismiss();
		LogUtil.eE("position",mComboBoxTapBarBeen.get(position).getType());
		LogUtil.eE("position",mComboBoxTapBarBeen.get(position).getUrl());
		if(mComboBoxTapBarBeen.get(position).getType().equals("saoyisao")){
			JumpActivityUtils.getInstance().jumpZXing(mContext);
		}else if(mComboBoxTapBarBeen.get(position).getType().equals("zhifu")){
			JumpActivityUtils.getInstance().jumpWebView(mContext,mComboBoxTapBarBeen.get(position).getUrl(),mComboBoxTapBarBeen.get(position).getTitle());
		}else if(mComboBoxTapBarBeen.get(position).getType().equals("baozhang")){
			String id = (String) SharedPreferencesUtils.getParam(mContext,HomeUtil.mUseridDB, "");
			String token = (String) SharedPreferencesUtils.getParam(mContext,HomeUtil.mTokenDB, "");
			String url= mComboBoxTapBarBeen.get(position).getUrl()+"?id="+id+"&token="+token;
			LogUtil.eE("Gson",url);
			JumpActivityUtils.getInstance().jumpWebView(mContext,url,mComboBoxTapBarBeen.get(position).getTitle());
		}
	}
}
